/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.assets.loader
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import ua.kiev.gcore.assets.event.AssetEvent;
	import ua.kiev.gcore.assets.storage.AssetItem;
	import ua.kiev.gcore.assets.storage.AssetsItemStorage;
	import ua.kiev.gcore.assets.storage.AssetsStorage;

	public class AssetLoader extends EventDispatcher
	{
		public var asset:AssetItem;
		private var loader:Loader;
		
		public var target:Sprite;
		public var mock:DisplayObject;
		public var error:DisplayObject;
		public var loadData:DisplayObject;
		public var completeFunction:Function;
		public var mainFrame:String;
		
		public function AssetLoader(target:Sprite = null, mock:DisplayObject = null, error:DisplayObject = null, completeFunction:Function = null, asset:AssetItem = null, mainFrame:String = null)
		{
			this.target = target;
			this.mock = mock;
			this.error = error;
			this.completeFunction = completeFunction;
			this.asset = asset;
			this.mainFrame = mainFrame;
		}
		
		public function load(asset:AssetItem):void{
			this.asset = asset;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetLoadeComplete);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, assetLoadeError);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, assetLoadeError);
			try{
				loader.load(new URLRequest(asset.path), new LoaderContext(true, ApplicationDomain.currentDomain));
			}catch(error:Error){
				trace("[AssetLoader] loade error id: " + asset.id + " url: "  + asset.path);
				trace(error.message);
				trace("\n");
				var assetEvent:AssetEvent = new AssetEvent(AssetEvent.ASSET_LOAD_ERROR);
				assetEvent.asset = asset;
				dispatchEvent(assetEvent);
			}
		}
		
		public function destroy():void{
			if(loader){
				if(loader.contentLoaderInfo.hasEventListener(Event.COMPLETE)){
					loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, assetLoadeComplete);
				}
				if(loader.contentLoaderInfo.hasEventListener(SecurityErrorEvent.SECURITY_ERROR)){
					loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, assetLoadeError);
				}
				if(loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)){
					loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, assetLoadeError);
				}
			}
		}
		
		private function assetLoadeComplete(event:Event):void{
			trace("[AssetLoader] loade complete id: " + asset.id + " url: "  + asset.path);
			trace("\n");
			loadData = loader.content;			
			var assetEvent:AssetEvent = new AssetEvent(AssetEvent.ASSET_LOAD_COMPLETE);
			assetEvent.asset = asset;
			dispatchEvent(assetEvent);			
		}
		
		private function assetLoadeError(event:ErrorEvent):void{
			trace("[AssetLoader] loade error id: " + asset.id + " url: "  + asset.path);
			trace(event.text);
			trace("\n");
			var assetEvent:AssetEvent = new AssetEvent(AssetEvent.ASSET_LOAD_ERROR);
			assetEvent.asset = asset;
			dispatchEvent(assetEvent);	
		}
		
	}
}